package com.ymatou.operate.trading.api.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.experimental.categories.Category;

import com.ymatou.operate.trading.api.parameter.AppOrderPostPayBean;
import com.ymatou.operate.trading.api.service.AppOrderPostPayCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.common.enums.PayMethodEnum;
import com.ymttest.common.enums.PlatformEnum;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.constant.ECode;

/**
 * 支付订单补款 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_AppOrderPostPay {

	private static AppOrderPostPayBean apporderpostpaybean;
	private static AppOrderPostPayCall apporderpostpaycall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("支付订单补款");
	}

	@Before
	public void caseUp() {

		apporderpostpaybean = new AppOrderPostPayBean();
		apporderpostpaycall = new AppOrderPostPayCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(apporderpostpaycall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
 
	//@Test 
	@Category(P1.class)
	@TestCase 
	public void Tc_AppOrderPostPay_001() {
		Logger.start(true,"支付订单补款成功");
		try {
			// 创建订单
			Map map = new OrderCallService().appSaveOrder();
			int orderId = (Integer) map.get("orderId");
			int sellerId = (Integer) map.get("sellerId");
			int userId = (Integer) map.get("userId");
			int tradingId = (Integer) map.get("tradingId");
			DecimalFormat df = new DecimalFormat("0.00");// 保留两位小数
			BigDecimal totalPrice = (BigDecimal) map.get("totalPrice");// 支付金额
			// 支付前账户金额
			HashMap<String, Integer> m1 = new HashMap<String, Integer>();
			m1.put("UserId", userId);
			BigDecimal price1 = (BigDecimal) new DefaultWapper().selectAccountInfo(m1).get(0).get("fAvailAmount");
			// 支付
			new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);			
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);
			// 卖家申请补款
			new OrderCallService().applyForPostPay(orderId, sellerId, totalPrice);
			// 参数
			apporderpostpaybean.setCallBackUrl("http://www.baidu.com");
			apporderpostpaybean.setOrderId(orderId);
			apporderpostpaybean.setPayMethod(PayMethodEnum.Alipay);
			apporderpostpaybean.setTradingPassword("abc123");
			apporderpostpaybean.setUseCashAmount(totalPrice);
			BigDecimal GiftAmount = new BigDecimal(0);
			apporderpostpaybean.setUseGiftAmount(GiftAmount);
			apporderpostpaybean.setUsePlatform(PlatformEnum.Shangou_APP);
			apporderpostpaybean.setUserIp("127.0.0.1");
			// 发送请求
			apporderpostpaycall.setData(apporderpostpaybean);
			apporderpostpaycall.callService();
			Logger.comment("apporderpostpaycall send:" + apporderpostpaycall.getEntityString());
			Logger.comment("apporderpostpaycall response:" + apporderpostpaycall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, apporderpostpaycall.getCode(), "验证Code");
			// 数据库验证
			// 支付后金额
			Thread.sleep(1000);
			BigDecimal price2 = (BigDecimal) new DefaultWapper().selectAccountInfo(m1).get(0).get("fAvailAmount");
			HashMap<String, Integer> m = new HashMap<String, Integer>();
			m.put("OrderId", orderId);
			m.put("TradingStatus", 17);
			m.put("TraddingId", orderId);
			m.put("PaidInFull", 1);
			m.put("UserId", userId);
			Logger.verifyEquals(1, new DefaultWapper().selectOrder(m).size(), "验证订单表记录");
			Logger.verifyEquals(2, new DefaultWapper().selectTradingStatus(m).size(), "验证交易表记录");
			Logger.verifyEquals(totalPrice.multiply(new BigDecimal(2)), price1.subtract(price2), "验证账户金额");
			Logger.verifyEquals(1, new DefaultWapper().selectOrderPostPayByOrderId(orderId).get(0).get("iAction"),
					"验证补款Action状态");
			HashMap<String, Integer> tnm = new HashMap<String, Integer>();
			tnm.put("OrderId", orderId);
			// 确认接单 发起补款 付款 补款
			//Logger.verifyEquals(4, new DefaultWapper().selectTradingNotifaction(tnm).size(), "验证交易通知表");
			//Logger.debug(new DefaultWapper().selectTradingNotifaction(tnm));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}